﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class CT_HoaDonDAO
    {
        public List<CT_HoaDonDTO> LayDanhSachCTHoaDon(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachCTHoaDon", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<CT_HoaDonDTO> list = new List<CT_HoaDonDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CT_HoaDonDTO cthd = new CT_HoaDonDTO();
                    cthd.MaCTHoaDon = dt.Rows[i]["MCTHD"].ToString();
                    cthd.MaHoaDon = dt.Rows[i]["MHD"].ToString();
                    cthd.MaMatHang =dt.Rows[i]["MMH"].ToString();
                    cthd.SoLuongBan =  (Int32)dt.Rows[i]["SoLuongBan"];
                    cthd.DonGiaBan = (float)dt.Rows[i]["DonGiaBan"];
                    cthd.MaDonVi = dt.Rows[i]["MDV"].ToString();
                    cthd.ThanhTien = (float)dt.Rows[i]["ThanhTien"];

                    cthd.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    list.Add(cthd);
                }
            }
            return list;
        }
        public string ThemCTHD(CT_HoaDonDTO cthd, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemCTHoaDon", new SqlParameter("@maHD", cthd.MaHoaDon),
                                                             new SqlParameter("@maMH", cthd.MaMatHang),
                                                             new SqlParameter("@soLuong", cthd.SoLuongBan),
                                                             new SqlParameter("@donGia", cthd.DonGiaBan),
                                                             new SqlParameter("@maDV", cthd.MaDonVi), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaCTHoaDon(CT_HoaDonDTO cthd,string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaCTHoaDon", new SqlParameter("@maCTHD", cthd.MaCTHoaDon),
                                                             new SqlParameter("@maHD", cthd.MaHoaDon),
                                                             new SqlParameter("@maMH", cthd.MaMatHang),
                                                             new SqlParameter("@soLuong", cthd.SoLuongBan),
                                                             new SqlParameter("@donGia", cthd.DonGiaBan),
                                                             new SqlParameter("@maDV", cthd.MaDonVi),
                                                              new SqlParameter("@daXoa", cthd.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaCTHoaDon(CT_HoaDonDTO cthd,string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaCTHoaDon", new SqlParameter("@maCTHD", cthd.MaCTHoaDon), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}